Thread: So, no overloading [][]?

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    129

    So, no overloading [][]?

    I have a 1D array that is treated as a 2D (long story). Is there a way to get calls like array[i][j] work? operator[][] (int x, int y) gives me a syntax error...

  2. #2
    Registered User dharh's Avatar
    Join Date
    Jan 2002
    Posts
    51
    If it is indeed a 1d array then id have to say it cant be called using [][] what would the second be pointing to? Can you give us a better example of what your trying to do? Why you want a 1d array to be treated as a 2d array?

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    129
    Well, you can overload () for it and by that way treat it as 2D. And as I said, it's a long story...

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Foundations:

    You have a class. This class should be indexable by double index ( [i][j] ). As a single operator, as you have experienced, it isn't possible. However, if you think it's worth the time, you can do the following:

    Create a function that does what you need as a normal member function "Indexer( int x, int y )".

    Create a single indexer ( [] ), that returns another object of a user defined class. This class defines another indexer that returns the parents class' Indexer function.

    Example:

    PHP Code:
    #include <stdio.h>

    class UsesDoubleIndexer
    {
        public:
        class 
    dblidx
        
    {
            private:
                
    int m_x;
                
    UsesDoubleIndexerm_Parent;
            public:
                
    dblidxint xUsesDoubleIndexerParent )
                {
                    
    m_x x;
                    
    m_Parent Parent;
                }
                
                
    int operator [] ( int y )
                {
                    return 
    m_Parent.Indexerm_x);
                }
        }

        
    dblidx operator [] ( int x )
        {
            return 
    dblidxxthis );
        }

        
    int Indexerint xint y )
        {
            
    // your indexer function here
            
    return 2*x+y;
        }
    };

    int main()
    {
        
    UsesDoubleIndexer udi;

        
    printf("%d"udi[0][3] );

        return 
    0;

    I've no compiler handy... so this is just an educated guess
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading operators
    By ugmusicbiz in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2009, 01:41 PM
  2. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 02:14 AM
  3. Operator= overloading from another type
    By g4j31a5 in forum C++ Programming
    Replies: 4
    Last Post: 10-04-2006, 08:15 PM
  4. overloading operator problems
    By almich in forum C++ Programming
    Replies: 2
    Last Post: 07-26-2004, 04:10 PM
  5. operator overloading
    By blue_gene in forum C++ Programming
    Replies: 6
    Last Post: 04-29-2004, 04:06 PM